package com.hitrust.gs.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import system.LoginContext;

import com.hitrust.gs.bean.GSOrderAnaDetailBean;
import com.hitrust.gs.bean.GSUserBean;
import com.hitrust.gs.business.GSOrderProcessor;
import com.hitrust.gs.business.GSUniteGwProcessor;
import com.hitrust.gs.dao.GSUserDAO;
import com.hitrust.gs.form.GSOrderQueryForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.DefineDic;
import com.hitrust.gs.util.page.ResultProcess;
import com.hitrust.gs.vo.GSOrderVO;

public class GSOrderQueryConfirmExportAction  extends GSBaseAction {

	public String execute(GSBaseForm aForm) throws Exception {
		 
        GSOrderQueryForm tForm = (GSOrderQueryForm)aForm;
        
        //取得页面参数组成订单VO对象
        GSOrderVO tOrderVO = new GSOrderVO();
        tOrderVO.orderid = tForm.orderId;
        tOrderVO.orderDetailStatus = tForm.orderDetailStatus;
        tOrderVO.orderDeptId = tForm.orderDeptId;
        tOrderVO.concentricityId = tForm.concentricityId;
        tOrderVO.manufInfo = tForm.manufInfo;
        tOrderVO.orderCreateDateLowLimie = tForm.orderCreateDateStr;
        tOrderVO.orderCreateDateUprLimie = tForm.orderCreateDateEnd;
        tOrderVO.drawingpiececode = tForm.drawingpiececode;
        
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		//ServletOutputStream out = null;
		ServletOutputStream out = null;
		HSSFWorkbook wb = null;
        
        try{
            
            //取得当前操作员信息
            LoginContext context = (LoginContext)aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
            //取得可操作企业及下级企业的订货地址
            GSUniteGwProcessor unitePro=new GSUniteGwProcessor();
            ArrayList wareListShow=unitePro.getWareHouseLevelName(context.getLoginWarehouseCode(), context.getLoginWarehouseCode());
            aForm.getRequest().setAttribute("orderdept",wareListShow);
            //取得分页参数
            //String[] tStr = this.getPageParams(tForm,tOrderVO);
            //查询订单
            GSOrderProcessor tProc = new GSOrderProcessor();
            ArrayList tList = tProc.getOrderDetailByOpt(tOrderVO,1,99999999);
            ResultProcess tRsProc = new ResultProcess();
            tRsProc.setResult(tList);
            
            //tForm.getRequest().setAttribute("order",tRsProc);
            //存储分页参数
            //tForm.getRequest().setAttribute("pageSize",tStr[1]);
            //tForm.getRequest().setAttribute("totalRecords",tStr[2]);
            
            String xls = "Order.xls";
            aForm.getResponse().setContentType("application/ms-excel");

    		aForm.getResponse().setHeader("Content-disposition", "attachment; filename=" + xls);
    		out = aForm.getResponse().getOutputStream();
    		wb = this.getExportExcel(tRsProc.getAllRecoreds(), aForm.getRequest());
    		wb.write(out);
    		
        }catch(Exception e){
            tForm.getRequest().setAttribute("action","A100401.do");
            throw e;
        }finally{
        	try {
				if (bos != null){
					bos.flush();
					bos.close();
				}
				if (bis != null){
					bis.close();
				}
				if (out != null){
					out.flush();
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
        return null;
	}

	private HSSFWorkbook getExportExcel(Collection list, HttpServletRequest request)throws Exception {


		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String strDate = sdf.format(new Date());
	
			HSSFWorkbook wb = new HSSFWorkbook();
			HSSFCellStyle titleStyle = wb.createCellStyle();
			titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平居中
			titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
			HSSFSheet sheet = wb.createSheet();
			
			int fistColumn = 0;					
			
			HSSFRow titleRow = sheet.createRow((short) fistColumn);
			
			HSSFCell titlecell = titleRow.createCell((short) 0);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("申购单明细编号");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 1);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("集中编码");
			titlecell.setCellStyle(createTopTitleStyle(wb));
					
			titlecell = titleRow.createCell((short) 2);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("产品名称");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 3);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("供应商");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 4);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("规格");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 5);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("型号");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 6);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("品牌");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 7);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("图件号");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 8);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("订货数量");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 9);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("申购单明细状态");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 10);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("要求送货时间");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 11);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("订货单位");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 12);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("价格（元）");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 13);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("总价（元）");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 14);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("结算模式");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 15);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("送货单号");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 16);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("创建时间");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 17);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("创建人");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			Iterator iter = list.iterator();
			int row = 1; 
			while(iter.hasNext()) {
				GSOrderAnaDetailBean temp = (GSOrderAnaDetailBean)iter.next();
				HSSFRow deptrow = sheet.createRow((short) row);
				
				HSSFCell cell = deptrow.createCell((short) 0 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(temp.getGs_order_detail_id())){
					cell.setCellValue(temp.getGs_order_detail_id());
				}
				
				cell = deptrow.createCell((short) 1 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(temp.getConcentricity_code())){
					cell.setCellValue(temp.getConcentricity_code());
				}		
				
				cell = deptrow.createCell((short) 2 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(temp.getName_product())){
					cell.setCellValue(temp.getName_product());
				}
				
				cell = deptrow.createCell((short) 3 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(temp.getName())){
					cell.setCellValue(temp.getName());
				}
				
				cell = deptrow.createCell((short) 4 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(temp.getSpec())){
					cell.setCellValue(temp.getSpec());
				}
				
				cell = deptrow.createCell((short) 5 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(temp.getModel())){
					cell.setCellValue(temp.getModel());
				}
				
				cell = deptrow.createCell((short) 6 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(temp.getProduct_brand())){
					cell.setCellValue(temp.getProduct_brand());
				}
				
				cell = deptrow.createCell((short) 7 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(temp.getDrawing_piece_code())){
					cell.setCellValue(temp.getDrawing_piece_code());
				}
				
				cell = deptrow.createCell((short) 8 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);			
				cell.setCellValue(com.hitrust.gs.util.lang.StringUtils.formatEDouble(temp.getGs_order_count(),2)+temp.getProduct_unit());
				
				cell = deptrow.createCell((short) 9 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(GSBaseManager.getOrderDetailStatusName(temp.getGs_order_detail_status()))){
					cell.setCellValue(GSBaseManager.getOrderDetailStatusName(temp.getGs_order_detail_status()));
				}
				
				cell = deptrow.createCell((short) 10 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(temp.getGs_deliver_goods_date())){
					cell.setCellValue(temp.getGs_deliver_goods_date());
				}
				
				cell = deptrow.createCell((short) 11 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(temp.getName_warehouse())){
					cell.setCellValue(temp.getName_warehouse());
				}
				
				cell = deptrow.createCell((short) 12 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(com.hitrust.gs.util.lang.StringUtils.formatEDouble(temp.getGs_goods_unitprice(),4))){
					cell.setCellValue(com.hitrust.gs.util.lang.StringUtils.formatEDouble(temp.getGs_goods_unitprice(),4));
				}
				
				cell = deptrow.createCell((short) 13 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(com.hitrust.gs.util.lang.StringUtils.formatEDouble(temp.getGs_order_count()*temp.getGs_goods_unitprice(),4))){
					cell.setCellValue(com.hitrust.gs.util.lang.StringUtils.formatEDouble(temp.getGs_order_count()*temp.getGs_goods_unitprice(),4));
				}
				
				cell = deptrow.createCell((short) 14 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(DefineDic.getBalanceMode(temp.getGs_balance_mode()))){
					cell.setCellValue(DefineDic.getBalanceMode(temp.getGs_balance_mode()));
				}
				
				cell = deptrow.createCell((short) 15 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(temp.getGs_deliver_goods_id())){
					cell.setCellValue(temp.getGs_deliver_goods_id());
				}
				
				cell = deptrow.createCell((short) 16 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(temp.getGs_order_create_date())){
					cell.setCellValue(temp.getGs_order_create_date());
				}
				
				cell = deptrow.createCell((short) 17 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(temp.getGs_recorder_id())){
					GSUserDAO udao=new GSUserDAO();
					GSUserBean user=udao.queryById(temp.getGs_recorder_id());
					if(user!=null){
						cell.setCellValue(user.getName());
					}
					
				}
				row ++;
			}		
		return wb;	
	}

	public static HSSFCellStyle createTopTitleStyle(HSSFWorkbook wb){
		HSSFCellStyle cellstyle = wb.createCellStyle();
		cellstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直   
		cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平   
		return cellstyle;
	}
}
